package com.example.rabbitmqboot.converter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.converter.MessageConversionException;


@Slf4j
//文件消息转换器
public class FileMessageConverter implements MessageConverter {


    @Override
    public Message toMessage(Object object, MessageProperties messageProperties) throws org.springframework.amqp.support.converter.MessageConversionException {
        throw new MessageConversionException("无法转成文件对象");
    }

    @Override
    //message转成其他对象
    public Object fromMessage(Message message) {

        log.info("文件消息转换器开始重构");

        MessageProperties messageProperties = message.getMessageProperties();
        String contentType = messageProperties.getContentType();
        switch (contentType) {
            case "application/pdf":
                message.getMessageProperties().setContentType("pdf");
                break;
            case "image":
                message.getMessageProperties().setContentType("png");
                break;
        }

        log.info("文件消息转换器重构完毕");
        return message;
    }

}
